package netty.embeddedChannel.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @描述:
 * @创建人: 涂有(tuyou_cd @ keruyun.com)
 * @创建时间: 2019-08-23
 */
public class FixLengthFrameDecoder extends ByteToMessageDecoder {

    private int frameLength;

    public FixLengthFrameDecoder(int frameLength) {
        this.frameLength = frameLength;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        while (in.readableBytes() >= frameLength) {
            ByteBuf frame = in.readBytes(frameLength);
            out.add(frame);
        }
    }
}
